#include "CToolbar.h"

CToolbar::CToolbar() {
  m_hToolbar = NULL;
}

void CToolbar::SetHandle(HWND hToolbar) {
  m_hToolbar = hToolbar;
}

HWND CToolbar::GetHandle() {
  return m_hToolbar;
}

HWND CToolbar::Create(DWORD dwStyle, HWND hParentWnd, UINT nID) {
  HINSTANCE hInstance;
  HRSRC     hFound;
  HGLOBAL   hResource;
  HBITMAP   hBitmap;
  HWND      hToolbar;
  TB_DATA  *pData;
  UINT     *pIDs;
  TBBUTTON *pButtons;

  // get module instance
  hInstance = GetModuleHandle(NULL);

  // find the resource
  if ((hFound = FindResource(hInstance, MAKEINTRESOURCE(nID),
          MAKEINTRESOURCE(241))) == NULL) {
    return FALSE;
  }

  // load toolbar resource
  if ((hResource = LoadResource(hInstance, hFound)) == NULL) {
    return FALSE;
  }

  // load toolbar bitmap
  if ((hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(nID))) == NULL) {
    return FALSE;
  }

  // lock resource
  pData    = (TB_DATA*)LockResource(hResource);
  pButtons = (TBBUTTON*)malloc(sizeof(TBBUTTON) * pData->uItemCount);
  pIDs     = pData->uIDs;

  int nBitmaps = 0;

  // create toolbar structure
  for (int i = 0; i < pData->uItemCount; i++) {
    // special treatment
    if (pIDs[i] == 0) {
      pButtons[i].iBitmap = 0;
      pButtons[i].iString = 0;
      pButtons[i].fsStyle = TBSTYLE_SEP;
    } else {
      pButtons[i].iBitmap  = nBitmaps;
      pButtons[i].iString  = nBitmaps++;
      pButtons[i].fsStyle = TBSTYLE_BUTTON;
    }

    // common procedure
    pButtons[i].idCommand = pIDs[i];
    pButtons[i].fsState   = TBSTATE_ENABLED;
    pButtons[i].dwData    = 0;
  }

  m_hToolbar = CreateToolbarEx(hParentWnd, WS_CHILD | WS_VISIBLE |
      WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CCS_NODIVIDER | dwStyle, nID,
      nBitmaps, NULL, (UINT_PTR)hBitmap, pButtons, pData->uItemCount,
      pData->uWidth, pData->uHeight, pData->uWidth, pData->uHeight,
      sizeof(TBBUTTON));

  free(pButtons);
  UnlockResource(hResource);

  return m_hToolbar;
}

BOOL CToolbar::Destroy() {
  return DestroyWindow(m_hToolbar);
}

INT CToolbar::AddBitmap(int nButtons, LPTBADDBITMAP lpBitmap) {
  return ::SendMessage(m_hToolbar, TB_ADDBITMAP, nButtons, (LPARAM)lpBitmap);
}

INT CToolbar::AddButtons(UINT nNumButtons, LPTBBUTTON lpButtons) {
  return ::SendMessage(m_hToolbar, TB_ADDBUTTONS, nNumButtons,
      (LPARAM)lpButtons);
}

INT CToolbar::AddString(HINSTANCE hInstance, LPCTSTR lpStringName) {
  return ::SendMessage(m_hToolbar, TB_ADDSTRING, (WPARAM)hInstance,
      MAKELONG(lpStringName, 0));
}

INT CToolbar::AutoSize() {
  return ::SendMessage(m_hToolbar, TB_AUTOSIZE, 0, 0);
}

INT CToolbar::ButtonCount() {
  return ::SendMessage(m_hToolbar, TB_BUTTONCOUNT, 0, 0);
}

INT CToolbar::ButtonStructureSize(int cbSize) {
  return ::SendMessage(m_hToolbar, TB_BUTTONSTRUCTSIZE, cbSize, 0);
}

INT CToolbar::ChangeBitmap(int nButton, int nBitmapIndex) {
  return ::SendMessage(m_hToolbar, TB_CHANGEBITMAP, nButton,
      MAKELPARAM(nBitmapIndex, 0));
}

INT CToolbar::CheckButton(int nButton, BOOL bCheck) {
  return ::SendMessage(m_hToolbar, TB_CHECKBUTTON, nButton,
      MAKELONG(bCheck, 0));
}

INT CToolbar::CommandToIndex(int nButton) {
  return ::SendMessage(m_hToolbar, TB_BUTTONSTRUCTSIZE, nButton, 0);
}

INT CToolbar::Customize() {
  return ::SendMessage(m_hToolbar, TB_CUSTOMIZE, 0, 0);
}

INT CToolbar::DeleteButton(int nButton) {
  return ::SendMessage(m_hToolbar, TB_DELETEBUTTON, nButton, 0);
}

INT CToolbar::EnableButton(int nButton, BOOL bEnable) {
  return ::SendMessage(m_hToolbar, TB_DELETEBUTTON, nButton, bEnable);
}

INT CToolbar::GetAnchorHighlight() {
  return ::SendMessage(m_hToolbar, TB_GETANCHORHIGHLIGHT, 0, 0);
}

INT CToolbar::GetBitmap(int nButton) {
  return ::SendMessage(m_hToolbar, TB_GETBITMAP, nButton, 0);
}

INT CToolbar::GetBitmapFlags() {
  return ::SendMessage(m_hToolbar, TB_GETBITMAPFLAGS, 0, 0);
}

INT CToolbar::GetButton(int nButton, LPTBBUTTON lpButton) {
  return ::SendMessage(m_hToolbar, TB_GETBUTTON, nButton, (LPARAM)lpButton);
}

INT CToolbar::SetButtonInfo(int nID, LPTBBUTTONINFO lpButtonInfo) {
  return ::SendMessage(m_hToolbar, TB_SETBUTTONINFO, nID, (LPARAM)lpButtonInfo);
}

INT CToolbar::GetButtonInfo(int nID, LPTBBUTTONINFO lpButtonInfo) {
  return ::SendMessage(m_hToolbar, TB_GETBUTTON, nID, (LPARAM)lpButtonInfo);
}

INT CToolbar::SetButtonSize(int nButtonX, int nButtonY) {
  return ::SendMessage(m_hToolbar, TB_SETBUTTONSIZE, 0,
      MAKELONG(nButtonX, nButtonY));
}

INT CToolbar::GetButtonSize() {
  return ::SendMessage(m_hToolbar, TB_GETBUTTONSIZE, 0, 0);
}

INT CToolbar::GetButtonText(int nButton, LPSTR lpszText) {
  return ::SendMessage(m_hToolbar, TB_GETBUTTONTEXT, nButton, (LPARAM)lpszText);
}

INT CToolbar::SetColorScheme(LPCOLORSCHEME lpColorScheme) {
  return ::SendMessage(m_hToolbar, TB_SETCOLORSCHEME, 0, (LPARAM)lpColorScheme);
}

INT CToolbar::GetColorScheme(LPCOLORSCHEME lpColorScheme) {
  return ::SendMessage(m_hToolbar, TB_GETCOLORSCHEME, 0, (LPARAM)lpColorScheme);
}

INT CToolbar::SetDisabledImageList(HIMAGELIST hImageList) {
  return ::SendMessage(m_hToolbar, TB_SETDISABLEDIMAGELIST, 0,
      (LPARAM)hImageList);
}

INT CToolbar::GetDisabledImageList() {
  return ::SendMessage(m_hToolbar, TB_GETDISABLEDIMAGELIST, 0, 0);
}

INT CToolbar::SetExtendedStyle(DWORD dwExStyle) {
  return ::SendMessage(m_hToolbar, TB_SETEXTENDEDSTYLE, 0, dwExStyle);
}

INT CToolbar::GetExtendedStyle() {
  return ::SendMessage(m_hToolbar, TB_GETEXTENDEDSTYLE, 0, 0);
}

INT CToolbar::SetHotImageList(HIMAGELIST hImageList) {
  return ::SendMessage(m_hToolbar, TB_SETHOTIMAGELIST, 0, (LPARAM)hImageList);
}

INT CToolbar::GetHotImageList() {
  return ::SendMessage(m_hToolbar, TB_GETHOTIMAGELIST, 0, 0);
}

INT CToolbar::SetHotItem(int nHot) {
  return ::SendMessage(m_hToolbar, TB_SETHOTITEM, nHot, 0);
}

INT CToolbar::GetHotItem() {
  return ::SendMessage(m_hToolbar, TB_GETHOTITEM, 0, 0);
}

INT CToolbar::SetImageList(int nImageID, HIMAGELIST hImageList) {
  return ::SendMessage(m_hToolbar, TB_SETIMAGELIST, nImageID,
      (LPARAM)hImageList);
}

INT CToolbar::GetImageList() {
  return ::SendMessage(m_hToolbar, TB_GETIMAGELIST, 0, 0);
}

INT CToolbar::SetInsertMark(LPTBINSERTMARK lpMark) {
  return ::SendMessage(m_hToolbar, TB_SETINSERTMARK, 0, (LPARAM)lpMark);
}

INT CToolbar::GetInsertMark(LPTBINSERTMARK lptbInsertMark) {
  return ::SendMessage(m_hToolbar, TB_GETINSERTMARK, 0, (LPARAM)lptbInsertMark);
}

INT CToolbar::SetInsertMarkColor(COLORREF clrInsertMark) {
  return ::SendMessage(m_hToolbar, TB_SETINSERTMARKCOLOR, 0,
      (LPARAM)clrInsertMark);
}

INT CToolbar::GetInsertMarkColor() {
  return ::SendMessage(m_hToolbar, TB_GETINSERTMARKCOLOR, 0, 0);
}

INT CToolbar::GetItemRect(int nButton, LPRECT lpRect) {
  return ::SendMessage(m_hToolbar, TB_GETITEMRECT, nButton, (LPARAM)lpRect);
}

INT CToolbar::GetMaxSize(LPSIZE lpSize) {
  return ::SendMessage(m_hToolbar, TB_GETMAXSIZE, 0, (LPARAM)lpSize);
}

INT CToolbar::SetMetrics(LPTBMETRICS ptbMetrics) {
  return ::SendMessage(m_hToolbar, TB_SETMETRICS, 0, (LPARAM)ptbMetrics);
}

INT CToolbar::GetMetrics(LPTBMETRICS ptbMetrics) {
  return ::SendMessage(m_hToolbar, TB_GETMETRICS, 0, (LPARAM)ptbMetrics);
}

INT CToolbar::SetPadding(int nPaddingX, int nPaddingY) {
  return ::SendMessage(m_hToolbar, TB_SETPADDING, 0,
      MAKELPARAM(nPaddingX, nPaddingY));
}

INT CToolbar::GetPadding() {
  return ::SendMessage(m_hToolbar, TB_GETPADDING, 0, 0);
}

INT CToolbar::GetPressedImageList() {
  return ::SendMessage(m_hToolbar, TB_GETPRESSEDIMAGELIST, 0, 0);
}

INT CToolbar::GetRect(int nID, LPRECT lpRect) {
  return ::SendMessage(m_hToolbar, TB_GETRECT, nID, (LPARAM)lpRect);
}

INT CToolbar::SetRows(int nRows, BOOL bLarger, LPRECT lpRect) {
  return ::SendMessage(m_hToolbar, TB_SETROWS,
      MAKEWPARAM(nRows, bLarger), (LPARAM)lpRect);
}

INT CToolbar::GetRows() {
  return ::SendMessage(m_hToolbar, TB_GETROWS, 0, 0);
}

INT CToolbar::SetState(int nButton, UINT nState) {
  return ::SendMessage(m_hToolbar, TB_SETSTATE, nButton, MAKELONG(nState, 0));
}

INT CToolbar::GetState(int nButton) {
  return ::SendMessage(m_hToolbar, TB_GETSTATE, nButton, 0);
}

INT CToolbar::GetString(int nString, LPTSTR lpBuffer, int nLength) {
  return ::SendMessage(m_hToolbar, TB_GETSTRING,
      MAKEWPARAM(nLength, nString), (LPARAM)lpBuffer);
}

INT CToolbar::SetStyle(DWORD dwStyle) {
  return ::SendMessage(m_hToolbar, TB_SETSTYLE, 0, dwStyle);
}

INT CToolbar::GetStyle() {
  return ::SendMessage(m_hToolbar, TB_GETSTYLE, 0, 0);
}

INT CToolbar::GetTextRows() {
  return ::SendMessage(m_hToolbar, TB_GETTEXTROWS, 0, 0);
}

INT CToolbar::SetToolTips(HWND hwndToolTip) {
  return ::SendMessage(m_hToolbar, TB_SETTOOLTIPS, (WPARAM)hwndToolTip, 0);
}

INT CToolbar::GetTooltips() {
  return ::SendMessage(m_hToolbar, TB_GETTOOLTIPS, 0, 0);
}

INT CToolbar::SetUnicodeFormat(BOOL bUnicode) {
  return ::SendMessage(m_hToolbar, TB_SETUNICODEFORMAT, bUnicode, 0);
}

INT CToolbar::GetUnicodeFormat() {
  return ::SendMessage(m_hToolbar, TB_GETUNICODEFORMAT, 0, 0);
}

INT CToolbar::HideButton(int nButton, BOOL bShow) {
  return ::SendMessage(m_hToolbar, TB_HIDEBUTTON, nButton, MAKELONG(bShow, 0));
}

INT CToolbar::HitTest(LPPOINT lpPoint) {
  return ::SendMessage(m_hToolbar, TB_HITTEST, 0, (LPARAM)lpPoint);
}

INT CToolbar::Indeterminate(int nButton, BOOL bIndeterminate) {
  return ::SendMessage(m_hToolbar, TB_INDETERMINATE, nButton,
      MAKELONG(bIndeterminate, 0));
}

INT CToolbar::InsertButton(int nButton, LPTBBUTTON lpButton) {
  return ::SendMessage(m_hToolbar, TB_INSERTBUTTON, nButton, (LPARAM)lpButton);
}

INT CToolbar::InsertMarkHitTest(LPPOINT lpPoint, LPTBINSERTMARK lpMark) {
  return ::SendMessage(m_hToolbar, TB_INSERTMARKHITTEST, (WPARAM)lpPoint,
      (LPARAM)lpMark);
}

INT CToolbar::IsButtonChecked(int nButton) {
  return ::SendMessage(m_hToolbar, TB_ISBUTTONCHECKED, nButton, 0);
}

INT CToolbar::IsButtonEnabled(int nButton) {
  return ::SendMessage(m_hToolbar, TB_ISBUTTONENABLED, nButton, 0);
}

INT CToolbar::IsButtonHidden(int nButton) {
  return ::SendMessage(m_hToolbar, TB_ISBUTTONHIDDEN, nButton, 0);
}

INT CToolbar::IsButtonHighlighted(int nButton) {
  return ::SendMessage(m_hToolbar, TB_ISBUTTONHIGHLIGHTED, nButton, 0);
}

INT CToolbar::IsButtonIndeterminate(int nButton) {
  return ::SendMessage(m_hToolbar, TB_ISBUTTONINDETERMINATE, nButton, 0);
}

INT CToolbar::IsButtonPressed(int nButton) {
  return ::SendMessage(m_hToolbar, TB_ISBUTTONPRESSED, nButton, 0);
}

INT CToolbar::LoadImages(HINSTANCE hInstance, int nBitmapID) {
  return ::SendMessage(m_hToolbar, TB_LOADIMAGES, nBitmapID, (LPARAM)hInstance);
}

INT CToolbar::MapAccelerator(TCHAR chAccel, LPUINT pIDButton) {
  return ::SendMessage(m_hToolbar, TB_MAPACCELERATOR, chAccel,
      (LPARAM)pIDButton);
}

INT CToolbar::MarkButton(int nButton, BOOL bHighlight) {
  return ::SendMessage(m_hToolbar, TB_MARKBUTTON, nButton,
      MAKELONG(bHighlight, 0));
}

INT CToolbar::MoveButton(UINT nOldPos, UINT nNewPos) {
  return ::SendMessage(m_hToolbar, TB_MOVEBUTTON, nOldPos, nNewPos);
}

INT CToolbar::PressButton(int nButton, BOOL bPress) {
  return ::SendMessage(m_hToolbar, TB_PRESSBUTTON, nButton,
      MAKELONG(bPress, 0));
}

INT CToolbar::ReplaceBitmap(LPTBREPLACEBITMAP ptReplaceBitmap) {
  return ::SendMessage(m_hToolbar, TB_REPLACEBITMAP, 0,
      (LPARAM)ptReplaceBitmap);
}

INT CToolbar::SaveRestore(BOOL bSave, TBSAVEPARAMS * pSaveParams) {
  return ::SendMessage(m_hToolbar, TB_SAVERESTORE, bSave, (LPARAM)&pSaveParams);
}

INT CToolbar::SetAnchorHighlight(BOOL bAnchor) {
  return ::SendMessage(m_hToolbar, TB_SETANCHORHIGHLIGHT, bAnchor, 0);
}

INT CToolbar::SetBitmapSize(int nBitmapX, int nBitmapY) {
  return ::SendMessage(m_hToolbar, TB_SETBITMAPSIZE, 0,
      MAKELONG(nBitmapX, nBitmapY));
}

INT CToolbar::SetButtonWidth(int nMinimumX, int nMinimumY) {
  return ::SendMessage(m_hToolbar, TB_SETBUTTONWIDTH, 0,
      MAKELONG(nMinimumX, nMinimumY));
}

INT CToolbar::SetCmdID(UINT nIndex, UINT nCmdID) {
  return ::SendMessage(m_hToolbar, TB_SETCMDID, nIndex, nCmdID);
}

INT CToolbar::SetDrawTextFlags(DWORD dwMask, DWORD dwDTFlags) {
  return ::SendMessage(m_hToolbar, TB_SETDRAWTEXTFLAGS, dwMask, dwDTFlags);
}

INT CToolbar::SetIndent(int nIndent) {
  return ::SendMessage(m_hToolbar, TB_SETINDENT, nIndent, 0);
}

INT CToolbar::SetListGap(int nPixels) {
  return ::SendMessage(m_hToolbar, TB_SETLISTGAP, nPixels, 0);
}

INT CToolbar::SetMaxTextRows(int nMaxRows) {
  return ::SendMessage(m_hToolbar, TB_SETMAXTEXTROWS, nMaxRows, 0);
}

INT CToolbar::SetParent(HWND hwndParent) {
  return ::SendMessage(m_hToolbar, TB_SETPARENT, (WPARAM)hwndParent, 0);
}

INT CToolbar::SetPressedImageList(int nImageID, HIMAGELIST hImageList) {
  return ::SendMessage(m_hToolbar, TB_SETPRESSEDIMAGELIST, nImageID,
      (LPARAM)hImageList);
}

INT CToolbar::SetWindowTheme(LPWSTR pwVisualStyle) {
  return ::SendMessage(m_hToolbar, TB_SETWINDOWTHEME, 0, (LPARAM)pwVisualStyle);
}

